package com.wctj.api.security.filter;

import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.wctj.api.base.constant.JwtConstant;
import com.wctj.api.base.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


/**
 * @author carol
 */
@Slf4j
public class JwtParseHeaderFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
            HttpServletRequest req = (HttpServletRequest) request;
            String auth = req.getHeader(JwtConstant.HEADER_TOKEN_CODE);
            //如果没有auth 或者请求头里面已经有了用户名就不再解析
            if(StringUtils.isBlank(auth) || StringUtils.isNotBlank(req.getHeader(JwtConstant.HEADER_NAME_USER_ID))){
                chain.doFilter(request, response);
                return;
            }

            if(auth.startsWith(JwtConstant.TOKEN_PREFIX)){
                auth = auth.substring(JwtConstant.TOKEN_PREFIX.length());
            }
//            Map<String, String> payload = getPayload(auth);
            JWTPayload payload = JWTUtil.parseToken(auth).getPayload();

            HeaderMapRequestWrapper headerMapRequestWrapper = new HeaderMapRequestWrapper(req);
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_GROUP_ID, payload.getClaim(JwtConstant.CLAIMS_NAME_GROUP_ID));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_SYSTEM_CODE, payload.getClaim(JwtConstant.CLAIMS_NAME_SYSTEM_CODE));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_USERNAME, payload.getClaim(JwtConstant.CLAIMS_NAME_USERNAME));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_USER_ID, payload.getClaim(JwtConstant.CLAIMS_NAME_USER_ID));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_IS_ADMIN, payload.getClaim(JwtConstant.CLAIMS_NAME_IS_ADMIN));

            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_AID, payload.getClaim(JwtConstant.CLAIMS_NAME_AID));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_BID, payload.getClaim(JwtConstant.CLAIMS_NAME_BID));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_VAR1, payload.getClaim(JwtConstant.CLAIMS_NAME_VAR1));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_VAR2, payload.getClaim(JwtConstant.CLAIMS_NAME_VAR2));
            headerMapRequestWrapper.addHeader(JwtConstant.HEADER_NAME_VAR3, payload.getClaim(JwtConstant.CLAIMS_NAME_VAR3));

            chain.doFilter(headerMapRequestWrapper, response);
        }
    }


    private Map<String, String> getPayload(String auth) {
        String sub = auth.split("\\.")[1];
        return JsonUtils.readFromBytes(java.util.Base64.getUrlDecoder().decode(sub),Map.class);
    }

//    public static void main(String[] args) {
//        JwtParseHeaderFilter filter = new JwtParseHeaderFilter();
//        log.info("{}", filter.getPayload("eyJhbGciOiJFUzI1NiJ9.eyJzYyI6IlRNWSIsInN1YiI6Im5iZ0A4NTMxLmNvbSIsInVpZCI6NDE5LCJnaWQiOjEzODcsImlpZCI6Ijk0Mjg0Iiwicm4iOiLlpKfmtIvmtYvor5UiLCJleHAiOjE2NjQxODQwMjgsImlhdCI6MTY2NDE4MDQyOCwidGlkIjoxMDIsInV0IjoyfQ.SPBSozBXwYJclw55Y3OS19tDqUC6_-uZbWD8VQUnzVROCStuvts-m3Cp7sh9Y8ShwxwcsekBKY3qY25OhbYOmA"));
//    }
}
